﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using MpSoft.Library.BUSS.Categories;
using MpSoft.Library.DTO;
using MpSoft.Basic.Utils;
using MpSoft.Library.BUSS.Settings;

namespace MpSoft.Web.AdminCP.Controls.Content
{
    public partial class ArticleEdit : System.Web.UI.UserControl
    {
        #region Var

        private int categoryID = -1;
        private bool showlanguage = false;

        #endregion

        #region Method

        #region LanguageDefault

        private void BindControl()
        {
            SetControl();

            if (categoryID > 0)
            {
                var obj = CategoryBUSS.Category_GetByID(categoryID);
                if (obj != null)
                {
                    ddlCategory.SelectVlaue = obj.ParentID;
                    txtOrderSort.NumberIncremnet = obj.SortOrder;
                    chkIsActive.Checked = obj.IsActive;

                    contentLngDefault.Name = obj.CategoryName;
                    ddlMenu.SelectMenu = obj.MenuID;
                    ddlTypeOpen.SelectedValue = obj.Target;
                    contentLngDefault.MetaTitle = obj.MetaTitle;
                    contentLngDefault.MetaKeyWord = obj.MetaKeyword;
                    contentLngDefault.MetaDescription = obj.MetaDescription;
                    contentLngDefault.SeName = obj.SeName;
                    contentLngDefault.Content = obj.CategoryDESC;
                    txtLink.Text = obj.Link;
                    txtIcon.Text = obj.Icon;
                }
            }
        }

        private int SaveCategory()
        {

            var obj = new Library.DTO.Category();
            obj.CategoryID = categoryID;
            obj.ParentID = ddlCategory.SelectVlaue > 0 ? ddlCategory.SelectVlaue : 0;
            obj.Level = ddlCategory.Level;
            obj.SortOrder = txtOrderSort.NumberIncremnet;
            obj.IsActive = chkIsActive.Checked;

            obj.CategoryName = contentLngDefault.Name;
            obj.MenuID = ddlMenu.SelectMenu;
            obj.Target = ddlTypeOpen.SelectedValue;
            obj.TypeCode = "bai-viet";
            obj.MetaTitle = string.IsNullOrEmpty(contentLngDefault.MetaTitle) ? obj.CategoryName : contentLngDefault.Name;
            obj.MetaKeyword = contentLngDefault.MetaKeyWord;
            obj.MetaDescription = contentLngDefault.MetaDescription;
            obj.SeName = string.IsNullOrEmpty(contentLngDefault.SeName) ? CommonHelper.FillterChar(obj.CategoryName) : contentLngDefault.SeName;
            obj.CategoryDESC = contentLngDefault.Content;
            obj.Link = txtLink.Text;
            obj.Icon = txtIcon.Text;

            if (obj.CategoryID > 0)
                return CategoryBUSS.Category_Update(obj);
            else
            {
                obj.SeName = CategoryBUSS.Category_ExistsSeName(obj.SeName);
                return CategoryBUSS.Category_Add(obj);
            }
        }

        private void SetControl()
        {
            ddlCategory.Module = "bai-viet";
            showlanguage = true;
            contentLngDefault.HideDescription = true;
            contentLngDefault.NameNotNull = true;
            BindLanguage();
        }

        private void Message(string message)
        {
            MpSoft.Basic.Utils.CommonHelper.ResetForm(this.Controls);
            ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
        }

        #endregion

        #region Language

        protected string TabLanguage()
        {
            var list = LanguageBUSS.Language_Select(null);
            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var obj in list)
                    sb.Append(" <option value='#" + obj.LanguageCode + "'>" + obj.LanguageTitle + "</option>");
                return sb.ToString();
            }
            return "";
        }

        private void BindLanguage()
        {
            rpList.DataSource = LanguageBUSS.Language_Select(null);
            rpList.DataBind();
        }

        private void SaveLanguage(int categoryid)
        {
            if (categoryid < 1) return;
            if (rpList.Items.Count < 1) return;
            foreach (RepeaterItem item in rpList.Items)
            {
                var ContentLanguage = item.FindControl("ContentLanguage") as Modules.ContentLanguage;
                if (ContentLanguage != null)
                {
                    if (string.IsNullOrEmpty(ContentLanguage.Language))
                        continue;
                    var obj = CategoryLanguageBUSS.CategoryLanguage_ByID(categoryid, ContentLanguage.Language);
                    if (obj == null)
                        obj = new CategoryLanguage();
                    obj.LanguageCode = ContentLanguage.Language;
                    obj.CategoryID = categoryid;
                    obj.CategoryName = ContentLanguage.Name;
                    obj.CategoryDESC = ContentLanguage.Content;
                    obj.MetaTitle = string.IsNullOrEmpty(ContentLanguage.MetaTitle) ? obj.CategoryName : ContentLanguage.MetaTitle;
                    obj.MetaKeyword = ContentLanguage.MetaKeyWord;
                    obj.MetaDescription = ContentLanguage.MetaDescription;
                    obj.SeName = string.IsNullOrEmpty(contentLngDefault.SeName) ? CommonHelper.FillterChar(obj.CategoryName) : contentLngDefault.SeName;
                    if (obj.CLID > 0)
                        CategoryLanguageBUSS.CategoryLanguage_Update(obj);
                    else
                        CategoryLanguageBUSS.CategoryLanguage_Add(obj);
                }
            }
        }

        #endregion

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out categoryID);
            if (!IsPostBack)
                BindControl();
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            var _categoryid = SaveCategory();
            if (_categoryid > 0)
                Message("Cập nhật thành công!");
        }

        protected void lnkSaveExit_Click(object sender, EventArgs e)
        {
            var _categoryid = SaveCategory();
            if (_categoryid > 0)
                Response.Redirect("?mod=article");
        }

        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var ContentLanguage = e.Item.FindControl("ContentLanguage") as Modules.ContentLanguage;
            if (ContentLanguage != null)
            {
                var objLanguage = e.Item.DataItem as Language;
                ContentLanguage.Language = objLanguage.LanguageCode;
                ContentLanguage.HideDescription = true;
                var obj = CategoryLanguageBUSS.CategoryLanguage_ByID(categoryID, objLanguage.LanguageCode);
                if (obj != null)
                {
                    ContentLanguage.Name = obj.CategoryName;
                    ContentLanguage.Content = obj.CategoryDESC;
                    ContentLanguage.MetaTitle = obj.MetaTitle;
                    ContentLanguage.MetaKeyWord = obj.MetaKeyword;
                    ContentLanguage.MetaDescription = obj.MetaDescription;
                    ContentLanguage.SeName = obj.SeName;
                }
            }
        }

        #endregion

        #region Properties

        public bool ShowLanguage { get { return showlanguage; } }

        #endregion
    }
}